/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.ExamReportParams;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.ExamReportParamsRepository;
import me.zhengjie.modules.system.service.ExamReportParamsService;
import me.zhengjie.modules.system.service.dto.ExamReportParamsDto;
import me.zhengjie.modules.system.service.dto.ExamReportParamsQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.ExamReportParamsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhaoxiancheng
* @date 2023-10-30
**/
@Service
@RequiredArgsConstructor
public class ExamReportParamsServiceImpl implements ExamReportParamsService {

    private final ExamReportParamsRepository examReportParamsRepository;
    private final ExamReportParamsMapper examReportParamsMapper;

    @Override
    public Map<String,Object> queryAll(ExamReportParamsQueryCriteria criteria, Pageable pageable){
        Page<ExamReportParams> page = examReportParamsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(examReportParamsMapper::toDto));
    }

    @Override
    public List<ExamReportParamsDto> queryAll(ExamReportParamsQueryCriteria criteria){
        return examReportParamsMapper.toDto(examReportParamsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ExamReportParamsDto findById(String paramId) {
        ExamReportParams examReportParams = examReportParamsRepository.findById(paramId).orElseGet(ExamReportParams::new);
        ValidationUtil.isNull(examReportParams.getParamId(),"ExamReportParams","paramId",paramId);
        return examReportParamsMapper.toDto(examReportParams);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExamReportParamsDto create(ExamReportParams resources) {
        return examReportParamsMapper.toDto(examReportParamsRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ExamReportParams resources) {
        ExamReportParams examReportParams = examReportParamsRepository.findById(resources.getParamId()).orElseGet(ExamReportParams::new);
        ValidationUtil.isNull( examReportParams.getParamId(),"ExamReportParams","id",resources.getParamId());
        examReportParams.copy(resources);
        examReportParamsRepository.save(examReportParams);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String paramId : ids) {
            examReportParamsRepository.deleteById(paramId);
        }
    }

    @Override
    public void download(List<ExamReportParamsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ExamReportParamsDto examReportParams : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("报告名称", examReportParams.getParamName());
            map.put("特质综述", examReportParams.getTeZhiDesc());
            map.put("激励建议", examReportParams.getJiLiJianYi());
            map.put("著名人物", examReportParams.getZhuMingRen());
            map.put("发展建议", examReportParams.getFaZhanJianYi());
            map.put("1:成人。2:儿童", examReportParams.getParamType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}